{% extends 'oratk_app/master.html' %}
{% block tag_left %}
    {% include 'oratk_app/tag_left_user.html' %}
{% endblock %}
{% block title %}
    <title>ORATK</title>
{% endblock %}
{% block userinfo %}
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" >
        <h2 class="page-header">ORATK 工具</h2>
        <div class="table-responsive">
            {#            <form class="navbar-form navbar-left" action="/basic_app/user_search"  role="search" method="post">#}
            <form  name="Form"  class="navbar-form navbar-left" role="search" action="/oratk_app/oratk_select_new"  onsubmit="return validateForm()"  target="_blank" method="POST" >
                {#                <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"  aria-hidden="true" ></span><a href="/basic_app/user_add"> 添加用户</a></button>#}
                &nbsp 选择实例: &nbsp
                <select class="form-control" name="conn_string" id="conn_string">
                    <option id="def">--请选择实例名--</option>
                    {% for i in instanceinfo_result %}
                        <option id="a">{{ i.dbname }}={{ i.conn_string }}</option>
                    {% endfor %}
                </select>
                &nbsp 选择功能点: &nbsp
                <select class="form-control" name="tool_name" id="tool_name">
                    <option id="def">--请选择查询项--</option>
                    {% for i in oratk_tool_info %}
                        <option id="a">{{ i.name }}</option>
                    {% endfor %}
                </select>
                <button type="button" class="btn btn-default" id="btSearch" >查询(本页展示）</button>
                <button type="submit" class="btn btn-default"  id="btSearch_new">查询(新页展示）</button>
                <div id='arg_tag'>&nbsp </div>
            </form>
        </div>
        <h2 class="page-header"></h2>
        <div id="tab_detail"></div>
    </div>
    </div>
{% endblock %}

{% block script %}
    <script>
        $('#tool_name').change(function () {
             console.log("123");
            var tool_name1 = $('#tool_name').find("option:checked").val()
            if (tool_name1 == '查询段信息') {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入要查询的对象名称: &nbsp<input  class='form-control'  name='arg1' id='arg1'  /> <input name='arg_count'   id='arg' value='1'  style='display:none;' /><div>")
            }
            else if (tool_name1 == '执行计划') {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入sqlid: &nbsp<input  class='form-control'  name='arg1'  id='arg1' /><input name='arg_count' id='arg' value='1'  style='display:none;' /> <div>")
            }
            else if (tool_name1 == '参数信息') {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入查询的参数信息： &nbsp<input  class='form-control'  name='arg1'  id='arg1' /><input name='arg_count' id='arg' value='1'  style='display:none;' /> <div>")
            }
            else if (tool_name1 == '会话详情') {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入查询的sid： &nbsp<input  class='form-control'  name='arg1'  id='arg1' /><input name='arg_count' id='arg' value='1'  style='display:none;' /> <div>")
            }
            else if (tool_name1 == '查询约束信息') {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入查询的约束名称： &nbsp<input  class='form-control'  name='arg1'  id='arg1' /><input name='arg_count' id='arg' value='1'  style='display:none;' /> <div>")
            }
            else if ((tool_name1 == '查询sqltext')||(tool_name1 == '查询sqltext_history')) {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入查询的sqlid： &nbsp<input  class='form-control'  name='arg1'  id='arg1' /><input name='arg_count' id='arg' value='1'  style='display:none;' /> <div>")
            }
            else if (tool_name1 == '会话详情_2') {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入查询的sid： &nbsp<input  class='form-control'  name='arg1'  id='arg1' />&nbsp 请输入查询的实例： &nbsp<input  class='form-control'  name='arg2'  id='arg2' /><input name='arg_count' id='arg' value='2'  style='display:none;' /> <div>")
            }
            else if (tool_name1 == '所涉及对象信息_table') {
                $('#label_arg').remove()
                $('#arg_tag').after(" <div class='form-group' id='label_arg'> &nbsp 请输入表名： &nbsp<input  class='form-control'  name='arg1'  id='arg1' />&nbsp 请输入用户名： &nbsp<input  class='form-control'  name='arg2'  id='arg2' /><input name='arg_count' id='arg' value='2'  style='display:none;' /> <div>")
            }
            else {
                $('#label_arg').remove()
            }

        })


        function validateForm() {
            var select_conn = $('#conn_string').find("option:checked").attr("id")
            var select_name = $('#tool_name').find("option:checked").attr("id")
            if (select_conn == 'def') {
                alert('请选择实例')
                return false;}
            else if (select_name == 'def' ) {
                alert('请选择查询项')
                return false;}
            var tool_name_arg_count = $("#arg").val();
            //当参数个数为1个时
            if (tool_name_arg_count == 1) {
                if ($('#arg1').val() == '' ){
                    alert ('请输出要查询的关键字')
                    return false;
                }}

        }




        $('#btSearch').click(function () {
            var select_conn = $('#conn_string').find("option:checked").attr("id")
            var select_name = $('#tool_name').find("option:checked").attr("id")
            console.log(select_conn)
            if (select_conn == 'def') {
                alert('请选择实例')
                return;
            } else if (select_name == 'def') {
                alert('请选择查询项')
                return;
            }
            //当查询项为:为一个参数时
            {#var tool_name_arg_count = $("#arg_1").attr('arg_count')#}
            var tool_name_arg_count = $("#arg").val();
            //当参数个数为1个时
            if (tool_name_arg_count == 1) {
                if ($('#arg1').val() == '' ){
                    alert ('请输出要查询的关键字')
                    return;
                }
                $.ajax({
                    type: "POST",
                    data: {
                        'conn_string': $('#conn_string').val(),
                        'tool_name': $('#tool_name').val(),
                        'arg1': $('#arg1').val(),
                        'arg_count':tool_name_arg_count,
                        'submit_id': 2
                    },
                    url: "/oratk_app/oratk_select_new", //后台处理函数的url
                    cache: false,
                    dataType: "html",
                    success: function (result) {
                        $("#tab_detail").html(result);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                })
            }
            else if (tool_name_arg_count == 2){
                if ($('#arg1').val() == '' ){
                    alert ('请输出要查询的关键字')
                    return;
                }
                if ($('#arg2').val() == '' ){
                    alert ('请输出要查询的关键字')
                    return;
                }
                $.ajax({
                    type: "POST",
                    data: {
                        'conn_string': $('#conn_string').val(),
                        'tool_name': $('#tool_name').val(),
                        'arg1': $('#arg1').val(),
                        'arg2': $('#arg2').val(),
                        'arg_count':tool_name_arg_count,
                        'submit_id': 2
                    },
                    url: "/oratk_app/oratk_select_new", //后台处理函数的url
                    cache: false,
                    dataType: "html",
                    success: function (result) {
                        $("#tab_detail").html(result);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                })
            }
            else {
                $.ajax({
                    type: "POST",
                    data: {
                        'conn_string': $('#conn_string').val(),
                        'tool_name': $('#tool_name').val(),
                        'submit_id': 2
                    },
                    url: "/oratk_app/oratk_select_new", //后台处理函数的url
                    cache: false,
                    dataType: "html",
                    success: function (result) {
                        $("#tab_detail").html(result);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                })
            }

        })

    </script>
{% endblock %}